<?php
/**
 * 首页控制器
 * @author jason <yangjs17@yeah.net> 
 * @version TS3.0
 */
class ReportAction extends Action {
    /**
     * 架构函数 
     * @access public
     */
    var $userGroup=0;
    var $classId=0;
    var $year=array();
    var $user=array();
    public function __construct() {
        $user=model("User")->getUserInfo($_SESSION["mid"]);
        $user_Group=model("UserGroupLink")->where("uid=".$user['uid'])->find();
        $this->userGroup=$user_Group["user_group_id"];
        if($user_Group["user_group_id"]==5){
            $Class=M("class")->where("admin_id=".$user['uid'])->find();
            $this->classId=$Class["class_id"];
            $this->assign('classId', $this->classId); //当前登陆人所属的班级id
        }
        $this->user=$user;
        $this->assign('userGroup', $this->userGroup); //当前登陆人的用户组关系
        $this->year=array(
            '0'=>date('Y'),
            '1'=>date('Y',strtotime("-1 year")),
            '2'=>date('Y',strtotime("-2 year")),
        );
    }
    /**
     * 我的首页
     *
     * @return void
     */
    public function index() {
        $this->assign("year",$this->year);
        $this->display();
    }
    //班级自测报告
    public function GradeSelfTestReport() {
        $this->assign("year",$this->year);
        $this->display();
    }
    //班级报告详情
    public function ClassReport(){
        $rpt_id=intval($_GET["rpt_id"]);
        $data=M("health_report")->where("type=4 and rpt_id=".$rpt_id)->find();
        $Heart_rate=array(0,0,0);
        $tid=M("report_data")->where("rpt_id=".$data["rpt_id"])->group("tid")->select();
        $tids = array();
        foreach ($tid as $key => $value) {
            $test_data=D("TestData")->where("is_del=0 and test_id=".$value["tid"].' and year='.$data['year'])->find();
            if($test_data) {
                $tids[] = $value;
            }
        }
        foreach ($tids as $key => $value) {
            $test_data=D("TestData")->where("is_del=0 and test_id=".$value["tid"].' and year='.$data['year'])->find();
            if($test_data["heart_rate"]<80){
                $Heart_rate[0]++;
            }
            if($test_data["heart_rate"]>=80 && $test_data["heart_rate"]<=120){
                $Heart_rate[1]++;
            }
            if($test_data["heart_rate"]>120){
                $Heart_rate[2]++;
            }
            $student=D("student")->where("stu_id=".$test_data["stu_id"])->find();
            $report_result=model("CalculationMethod")->getDeterminationStandard($test_data,$student["gender"],$student["birthday"]);
            $result[]=$report_result;
        }
        $sum=array(0,0,0,0,0);
        $height=array(0,0,0,0,0);
        $weight=array(0,0,0,0,0);
        $reentry_run=array(0,0,0,0,0);
        $standing_long_jump=array(0,0,0,0,0);
        $tennis_throw=array(0,0,0,0,0);
        $continuousJump=array(0,0,0,0,0);
        $flexion=array(0,0,0,0,0);
        $balanceBeam=array(0,0,0,0,0);
        foreach ($result as $key => $value) {
            if($value["result"]==4){
                $sum[0]++;//不合格
            }
            if($value["result"]==3){
                $sum[1]++;//合格
            }
            if($value["result"]==2){
                $sum[2]++;//良好
            }
            if($value["result"]==1){
                $sum[3]++;//优秀
            }
            if($value["result"]==0){
                $sum[4]++;//无法评级
            }
            if($value["height"]==1){
                $height[0]++;
            }
            if($value["height"]==2){
                $height[1]++;
            }
            if($value["height"]==3){
                $height[2]++;
            }
            if($value["height"]==4){
                $height[3]++;
            }
            if($value["height"]==5){
                $height[4]++;
            }
            if($value["is_obesity"]==1){
                $weight[0]++;
            }
            if($value["is_obesity"]==2){
                $weight[1]++;
            }
            if($value["is_obesity"]==3){
                $weight[2]++;
            }
            if($value["is_obesity"]==4){
                $weight[3]++;
            }
            if($value["is_obesity"]==5){
                $weight[4]++;
            }
            if($value["reentry_run"]==1){
                $reentry_run[0]++;
            }
            if($value["reentry_run"]==2){
                $reentry_run[1]++;
            }
            if($value["reentry_run"]==3){
                $reentry_run[2]++;
            }
            if($value["reentry_run"]==4){
                $reentry_run[3]++;
            }
            if($value["reentry_run"]==5){
                $reentry_run[4]++;
            }
            if($value["flexion"]==1){
                $flexion[0]++;
            }
            if($value["flexion"]==2){
                $flexion[1]++;
            }
            if($value["flexion"]==3){
                $flexion[2]++;
            }
            if($value["flexion"]==4){
                $flexion[3]++;
            }
            if($value["flexion"]==5){
                $flexion[4]++;
            }
            if($value["continuousJump"]==1){
                $continuousJump[0]++;
            }
            if($value["continuousJump"]==2){
                $continuousJump[1]++;
            }
            if($value["continuousJump"]==3){
                $continuousJump[2]++;
            }
            if($value["continuousJump"]==4){
                $continuousJump[3]++;
            }
            if($value["continuousJump"]==5){
                $continuousJump[4]++;
            }
            if($value["tennis_throw"]==1){
                $tennis_throw[0]++;
            }
            if($value["tennis_throw"]==2){
                $tennis_throw[1]++;
            }
            if($value["tennis_throw"]==3){
                $tennis_throw[2]++;
            }
            if($value["tennis_throw"]==4){
                $tennis_throw[3]++;
            }
            if($value["tennis_throw"]==5){
                $tennis_throw[4]++;
            }
            if($value["standing_long_jump"]==1){
                $standing_long_jump[0]++;
            }
            if($value["standing_long_jump"]==2){
                $standing_long_jump[1]++;
            }
            if($value["standing_long_jump"]==3){
                $standing_long_jump[2]++;
            }
            if($value["standing_long_jump"]==4){
                $standing_long_jump[3]++;
            }
            if($value["standing_long_jump"]==5){
                $standing_long_jump[4]++;
            }
            if($value["balanceBeam"]==1){
                $balanceBeam[0]++;
            }
            if($value["balanceBeam"]==2){
                $balanceBeam[1]++;
            }
            if($value["balanceBeam"]==3){
                $balanceBeam[2]++;
            }
            if($value["balanceBeam"]==4){
                $balanceBeam[3]++;
            }
            if($value["balanceBeam"]==5){
                $balanceBeam[4]++;
            }
        }
        $data["sum"]=$sum;
        $data["height"]=$height;
        $data["weight"]=$weight;
        $data["Heart_rate"]=$Heart_rate;
        $data["reentry_run"]=$reentry_run;
        $data["standing_long_jump"]=$standing_long_jump;
        $data["tennis_throw"]=$tennis_throw;
        $data["continuousJump"]=$continuousJump;
        $data["flexion"]=$flexion;
        $data["balanceBeam"]=$balanceBeam;
        $data["count"]=count($tids);
        $this->assign("data",$data);
        $this->display();
    }
    //个体报告
    public function OneReport() {
        //个体报告
        // $test = M('HealthReport')->where('type=5')->field('rpt_id,fid')->findAll();
        // foreach($test as &$val){
        //     $data['class_id'] = M('student')->where('stu_id='.$val['fid'])->getField('class_id');
        //     M('HealthReport')->where('rpt_id='.$val['rpt_id'])->save($data);
        //     echo M()->getLastSql().'<br>';
        // }

        $this->assign("year",$this->year);
        $this->display();
    }
    //个体报告
    public function OneSelfTestReport() {
        $this->assign("year",$this->year);
        $this->display();
    }
    //学生个体报告
    public function studentReport() {
        $rpt_id=intval($_GET["rpt_id"]);
        $report_data = M("health_report")->where("rpt_id=".$rpt_id)->find();

        $data=D("TestData")->where("is_del=0 and stu_id=".$report_data["fid"].' and year='.$report_data['year'] .' and class_type='.$report_data['class_type'].' and is_report='.$report_data['is_report'].' and test_number='.$report_data['test_type'])->find();

        $class_info = M('class')->where('class_id='.$data['class_id'])->find();

        

        $class_id = $data['class_id'];
        if($data["class_type"]==1){
            $data['class_type'] = "大班";
            if( M('class')->where( array('class_id'=>$class_id,'class_name' => array('like' ,'%大%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name1' =>  array('like' ,'%大%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name1'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name2' =>  array('like' ,'%大%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name2'];
            }
        }else if($data["class_type"]==2){
            $data['class_type'] = "中班";
            if( M('class')->where( array('class_id'=>$class_id,'class_name' =>  array('like' ,'%中%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name1' =>  array('like' ,'%中%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name1'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name2' =>  array('like' ,'%中%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name2'];
            }
        }else if($data["class_type"]==3){
            $data['class_type'] = "小班";
            if( M('class')->where( array('class_id'=>$class_id,'class_name' =>  array('like' ,'%小%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name1' =>  array('like' ,'%小%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name1'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name2' =>  array('like' ,'%小%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name2'];
            }
        }else{
            $data['class_type'] = "混龄班";
            $data['class_name'] = $class_info['class_name'];
        }

        $student=D("student")->getstudentById($report_data['fid']);
        $data["report_result"]=model("CalculationMethod")->getDeterminationStandard($data,$student["gender"],$student["birthday"]);
        $health_report=D("HealthReport")->where("rpt_id=".$rpt_id)->find();
        $data["rpt_name"]=$health_report["rpt_name"];
        $data["advice_img"]=$health_report["advice_img"];
        if($data["advice_img"]){
            $advice=explode(",", $data["advice_img"]);
            foreach ($advice as $key => $value) {
                $health_advice=M("health_advice")->where("is_del=0 and advice_id=".$value)->find();
                if($health_advice){
                    $health_advice["attach_image"]=getAttachUrlByAttachId($health_advice["attach_id"]);
                }
                $HealthAdvice[]=$health_advice;
            }
            $this->assign("HealthAdvice",$HealthAdvice);
        }
        $data["comment"]=$health_report["comment"];
        $data["rpt_id"]=$health_report["rpt_id"];
        $this->year=array(
            '0'=>$data['year'],
            '1'=>$data['year']-1,
            '2'=>$data['year']-2,
        );
        //查询过去两年的学生测试数据
        for ($i=1; $i <count($this->year); $i++) { 
            $where=array(
                'year'=>$this->year[$i],
                'stu_id'=>$student["stu_id"],
                'test_number'=>$health_report['test_type'],
                'is_report'=>$data['is_report'],
                'is_del'=>0,
            );
            $user= M("TestData")->where($where)->find();
            if($user){
                $user["report_result"]=model("CalculationMethod")->getDeterminationStandard($user,$student["gender"],$student["birthday"]);
            }
            $userLastreport[]=$user;
        }
        $test_project=M("test_project")->select();
        $yeararr=array(
            '0'=>$data['year']-2,
            '1'=>$data['year']-1,
            '2'=>intval($data['year']),
        );
        $this->assign("test_project",$test_project);
        $this->assign("data",$data);
        $this->assign("student",$student);
        $this->assign("userLastreport",$userLastreport);
        $this->assign("yeararr",$yeararr);
        $this->display();
    }
    public function Report() {
        $rpt_id=intval($_GET["rpt_id"]);
        $report_data = M("health_report")->where("rpt_id=".$rpt_id)->find();

        $data=D("TestData")->where("is_del=0 and stu_id=".$report_data["fid"].' and year='.$report_data['year'] .' and class_type='.$report_data['class_type'].' and is_report='.$report_data['is_report'].' and test_number='.$report_data['test_type'])->find();

        $class_info = M('class')->where('class_id='.$data['class_id'])->find();

        

        $class_id = $data['class_id'];
        if($data["class_type"]==1){
            $data['class_type'] = "大班";
            if( M('class')->where( array('class_id'=>$class_id,'class_name' => array('like' ,'%大%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name1' =>  array('like' ,'%大%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name1'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name2' =>  array('like' ,'%大%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name2'];
            }
        }else if($data["class_type"]==2){
            $data['class_type'] = "中班";
            if( M('class')->where( array('class_id'=>$class_id,'class_name' =>  array('like' ,'%中%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name1' =>  array('like' ,'%中%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name1'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name2' =>  array('like' ,'%中%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name2'];
            }
        }else if($data["class_type"]==3){
            $data['class_type'] = "小班";
            if( M('class')->where( array('class_id'=>$class_id,'class_name' =>  array('like' ,'%小%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name1' =>  array('like' ,'%小%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name1'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name2' =>  array('like' ,'%小%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name2'];
            }
        }else{
            $data['class_type'] = "混龄班";
            $data['class_name'] = $class_info['class_name'];
        }

        $student=D("student")->getstudentById($report_data['fid']);
        $data["report_result"]=model("CalculationMethod")->getDeterminationStandard($data,$student["gender"],$student["birthday"]);
        $health_report=D("HealthReport")->where("rpt_id=".$rpt_id)->find();
        $data["rpt_name"]=$health_report["rpt_name"];
        $data["advice_img"]=$health_report["advice_img"];
        if($data["advice_img"]){
            $advice=explode(",", $data["advice_img"]);
            foreach ($advice as $key => $value) {
                $health_advice=M("health_advice")->where("is_del=0 and advice_id=".$value)->find();
                if($health_advice){
                    $health_advice["attach_image"]=getAttachUrlByAttachId($health_advice["attach_id"]);
                }
                $HealthAdvice[]=$health_advice;
            }
            $this->assign("HealthAdvice",$HealthAdvice);
        }
        $data["comment"]=$health_report["comment"];
        $data["rpt_id"]=$health_report["rpt_id"];
        $this->year=array(
            '0'=>$data['year'],
            '1'=>$data['year']-1,
            '2'=>$data['year']-2,
        );
        //查询过去两年的学生测试数据
        for ($i=1; $i <count($this->year); $i++) { 
            $where=array(
                'year'=>$this->year[$i],
                'stu_id'=>$student["stu_id"],
                'test_number'=>$health_report['test_type'],
                'is_report'=>$data['is_report'],
                'is_del'=>0,
            );
            $user= M("TestData")->where($where)->find();
            if($user){
                $user["report_result"]=model("CalculationMethod")->getDeterminationStandard($user,$student["gender"],$student["birthday"]);
            }
            $userLastreport[]=$user;
        }
        $test_project=M("test_project")->select();
        $yeararr=array(
            '0'=>$data['year']-2,
            '1'=>$data['year']-1,
            '2'=>intval($data['year']),
        );
        $this->assign("test_project",$test_project);
        $this->assign("data",$data);
        $this->assign("student",$student);
        $this->assign("userLastreport",$userLastreport);
        $this->assign("yeararr",$yeararr);
        $this->display();
    }
    //学生报告评价（操作）
    public function doSaveEvaluation(){
        $rpt_id=intval($_POST["rpt_id"]);
        $advice_type=$_POST["advice_type"];
        $test_id=intval($_POST["test_id"]);
        $data['advice_img']=D("TestData")->getAdviceImage($advice_type);
        $data["status"]=2;
        if($_POST["comment"]){
            $data['comment']=$_POST["comment"];
        }
        $res= D("HealthReport")->where("rpt_id=".$rpt_id)->save($data);
        if($res !== false){
            if($test_id){
                D("TestData")->where("test_id=".$test_id)->save(array('status'=>2));
            }
            echo $res;exit();
        }else{
            echo  "操作失败，请重试";exit();
        }
    }
    /**
     * 取得班级类型获取班级列表
     * @param boolean $return 是否返回数据，如果不是返回，则会直接输出Ajax JSON数据
     * @return void|array
     */
    public function getGradeByClassType() {
        $where["is_del"]=0;
        $school=M("school")->where("fid=".$_SESSION['mid'])->find();
        $where["school_id"]=$school["id"];
        $where['class_type'] = $class_type = intval($_GET["class_type"]);
        $data = M("class")->where($where)->select();
        
        foreach ($data as &$val) {
            $class_id = $val['class_id'];
            if($val["class_type"]==1){
                if( M('class')->where( array('class_id'=>$class_id,'class_name' => array('like' ,'%大%班%') ) )->find() ) {
                    $val['class_name'] = $val['class_name'];
                }
                if( M('class')->where( array('class_id'=>$class_id,'class_name1' =>  array('like' ,'%大%班%') ) )->find() ) {
                    $val['class_name'] = $val['class_name1'];
                }
                if( M('class')->where( array('class_id'=>$class_id,'class_name2' =>  array('like' ,'%大%班%') ) )->find() ) {
                    $val['class_name'] = $val['class_name2'];
                }
            }else if($val["class_type"]==2){
                if( M('class')->where( array('class_id'=>$class_id,'class_name' =>  array('like' ,'%中%班%') ) )->find() ) {
                    $val['class_name'] = $val['class_name'];
                }
                if( M('class')->where( array('class_id'=>$class_id,'class_name1' =>  array('like' ,'%中%班%') ) )->find() ) {
                    $val['class_name'] = $val['class_name1'];
                }
                if( M('class')->where( array('class_id'=>$class_id,'class_name2' =>  array('like' ,'%中%班%') ) )->find() ) {
                    $val['class_name'] = $val['class_name2'];
                }
            }else if($data["class_type"]==3){
                if( M('class')->where( array('class_id'=>$class_id,'class_name' =>  array('like' ,'%小%班%') ) )->find() ) {
                    $val['class_name'] = $val['class_name'];
                }
                if( M('class')->where( array('class_id'=>$class_id,'class_name1' =>  array('like' ,'%小%班%') ) )->find() ) {
                    $val['class_name'] = $val['class_name1'];
                }
                if( M('class')->where( array('class_id'=>$class_id,'class_name2' =>  array('like' ,'%小%班%') ) )->find() ) {
                    $val['class_name'] = $val['class_name2'];
                }
            }else{
                $val['class_name'] = $val['class_name'];
            }
        }

        unset($where["class_type"]);
        $class_type1 = $class_type - 1;
        $class_type2 = $class_type - 2;
        $where["class_type"]  = array('in',"{$class_type1},{$class_type2}");
        $where["is_up"]       = array('neq' , 0);
        $data1 = M("class")->where($where)->findAll();

        foreach($data1 as &$val){
            if($val['is_up'] == 1 && ($class_type == 3 || $class_type == 2 ) ) {
                $val['class_name'] = $val['class_name'];
            }   

            if($val['is_up'] == 2 && $class_type == 3 ) {
                $val['class_name'] = $val['class_name'];
            }

            if($val['is_up'] == 2 && $class_type == 2 ) {
                $val['class_name'] = $val['class_name1'];
            }
        }
        
        if($data && $data1) {
            $datas = array_merge($data , $data1);
        }
        if($data && !$data1) {
            $datas = $data;
        }
        if(!$data && $data1) {
            $datas = $data1;
        }
        if(!$data && !$data1) {
            $datas = array();
        }

        if( isset($_GET['is_report']) && $_GET['test_number'] ) {
            if( $_GET['is_report'] == 1) {
                $map['is_report']   = 1;
            } else {
                $map['is_report']   = 0;
            }
            $map['test_number'] = $_GET['test_number'];
            $map['class_type']  = $class_type;
            $map['year']        = $_GET['year'];
            $map['is_del']      = 0;
            $class_ids = M('test_data')->where($map)->field('class_id')->select();

            $class_ids = getSubByKey($class_ids , 'class_id');
            $class_ids = array_unique($class_ids);
            foreach ($datas as $key=>$value) {
                if( !in_array($value['class_id'], $class_ids)) {
                    unset($datas[$key]);
                }
            }
        }
        
        $this->assign('listData', $datas);
        $this->assign('where', $where);
        $html = $this->fetch('grade_list');
        echo json_encode($html);
        exit();
    }
    /**
     * 取得学生列表
     * @param boolean $return 是否返回数据，如果不是返回，则会直接输出Ajax JSON数据
     * @return void|array
     */
    public function getStudentList() {
        $number=intval($_GET["number"]);
        $year=intval($_GET["year"]);
        $Class=intval($_GET["Class"]);
        $name = t($_GET["name"]);
        $where['is_report']=intval($_GET["is_report"]);
		$number && $where['test_number']=$number;
        //判断当前账号是园级还是班级账号
        $user_group_id = M('user_group_link')->where('uid='.$_SESSION['mid'])->find();
        if($year){
            $where["year"]=$year;
        }
        if($Class) {
            $where["class_id"] = $Class;
        }
        if($_GET['class_type']) {
            $where["class_type"] = intval($_GET['class_type']);
        }else {
            if($user_group_id['user_group_id'] == 5) {//班级
                $where['class_type'] = M('class')->where('`admin_id`='.$_SESSION['mid'])->getField('class_type');
            }
        }
        $where['is_del'] = 0;
		$test_data = M("test_data")->where($where)->field("test_id,stu_id")->select();


		if($test_data){
			foreach($test_data as &$val){
				$_map=array(
					'year'=>$year,
					'is_report'=>$where["is_report"],
                    'test_number'=>$number,
					//'class_type'=>$class_type,
					'stu_id'=>$val['stu_id']
				);
				//D("HealthReport")->addHeathReport($_map,$val["test_id"]);
			}
			$where["fid"]  = array('in' ,  getSubByKey($test_data,"stu_id") );
		}
		unset($where['test_number']);
		unset($where["class_id"]);
		
		if($name){
            $where["rpt_name"]=array("like",'%'.$name.'%');
            
            if($user_group_id['user_group_id'] == 4) {//园级
                $school_id = M("school")->where("fid=".$_SESSION['mid'])->find();
                //获取此学校所有学生
                $stu_id  = M("student")->where('school_id=' . $school_id['id'])->field('stu_id')->select();
                $where["fid"] = array('in' ,  getSubByKey($stu_id , "stu_id") );
            } elseif($user_group_id['user_group_id'] == 5) {//班级
                $class_id  = M("class")->where('`admin_id`=' . $_SESSION['mid'])->find();

                //获取此班级所有学生
                $stu_id  = M("student")->where('class_id=' . $class_id['class_id'])->field('stu_id')->select();
                $where["fid"] = array('in' ,  getSubByKey($stu_id , "stu_id") );
            }
			
        }
		if($number){
            $where['test_type']=$number;
        }
        
        $where["type"]=5;
        
        //判断是否存在班级报告结束
        if($test_data){
		unset($where['is_del']);
            $data = D("HealthReport")->where($where)->order('year desc')->findPage(20);
        }

        if ($data['data']) {
            $this->assign('listData', $data['data']);
            $this->assign('Class',$Class);
            $this->assign('number',$number);
            $this->assign('year',$year);
            $this->assign('name',$name);
            $html = $this->fetch('student_list');
            //$html.=$wh;
        } else {
            $html = $this->fetch('student_list');
            //$html.=$wh;
        }
        $data['data'] = $html;
        echo json_encode($data);
        exit();
    }
    /**
     * 取得班级数据
     * @param boolean $return 是否返回数据，如果不是返回，则会直接输出Ajax JSON数据
     * @return void|array
     */
    public function getList() {
        $number=$_GET["number"];
        $year=$_GET["year"] ? $_GET["year"] : date('Y' ,time());
        $where['type']=4;
        $where['is_report']=intval($_GET["is_report"]);
        if($number){
           $where['test_type']=$number;
        }
        if($year){
           $where['year']=$year;
        }
        $Class=$_GET["Class"];
        if($Class==0){
            $map['class_id'] = $this->classId;
            $Class = M('class')->where('class_id='.$map['class_id'])->getField('class_type');
        }else{
            $school=M("school")->where("fid=".$this->user['uid'])->find();
            $map['school_id']=$school["id"];
            //$map["class_type"]=$Class;
        }
        $class_id=M("class")->where($map)->field("class_id")->select();
        $class_id = getSubByKey($class_id,'class_id');
        $where['fid']=array("in",$class_id);
        $where['class_type'] = $Class;
        //判断是否存在班级报告开始
        $data = M("health_report")->where($where)->field("fid")->select();
        $fid_ids=getSubByKey($data,"fid");

        $map["class_type"]=$Class;
        $class_ids=M("class")->where($map)->field("class_id")->select();
        $class_ids = getSubByKey($class_ids,'class_id');
        foreach ($class_ids as $k => $v) {
            if(!in_array($v, $fid_ids) ){
                $_map=array(
                    'year'=>$where["year"],
                    'is_report'=>$where["is_report"],
                    'test_number'=>$where["test_type"],
                    'class_id'=>$v,
                    'is_del'=>0,
                );
                $test_ids=M("test_data")->where($_map)->field("test_id")->select();
                if($test_ids){
                    unset($_map["test_number"]);
                    unset($_map["class_id"]);
                    $_map["fid"]=$v;
                    $_map["time"]=time();
                    $_map["type"]=4;
                    $_map["class_type"]=$Class;
                    $_map["test_type"]=$where["test_type"];
                    $_map["rpt_name"]=D("HealthReport")->getGradeReportName($v,$where["is_report"],$where["test_type"]);
                    //TODO
                    // if($rpt_id=D("HealthReport")->add($_map)){
                    //    foreach ($test_ids as $t => $test) {
                    //        M("report_data")->add(array("tid"=>$test["test_id"],'rpt_id'=>$rpt_id));
                    //    }
                    // }
                }
            }
        }
        //判断是否存在班级报告结束
        $data = M("health_report")->where($where)->findPage(20);

        foreach($data['data'] as $key => $value) {
            $reportData=M("report_data")->where("rpt_id=".$value["rpt_id"])->group("tid")->select();
            //判断测试数据是否存在，存在则判断孩子是否存在，不存在则删除
            foreach ($reportData as $k => $v) {
                $test_data=M("test_data")->where("is_del=0 and test_id=".$v["tid"].' and year='.$year)->find();
                if($test_data){
                    if(!M("student")->where("is_del=0 and stu_id=".$test_data["stu_id"])->find()){
                         M("test_data")->where("test_id=".$test_data["test_id"])->save( array('is_del'=>1));
                         //M("report_data")->where("tid=".$test_data["test_id"])->delete();
                         unset($reportData[$k]);
                    }
                }else{
                    unset($reportData[$k]);
                }
            }
            $student=D("student")->where("is_del=0 and class_id=".$value["fid"])->select();
            $data['data'][$key]["sum"]=count($student);
            $data['data'][$key]["number"]=count($reportData);
        }
        if ($data['data']) {
            $this->assign('listData', $data['data']);
            $this->assign('Class',$Class);
            $this->assign('number',$number);
            $this->assign('year',$year);
            $html = $this->fetch('index_list');
        } else {
            $html = $this->fetch('index_list');
        }
        $data['data'] = $html;
        echo json_encode($data);
        exit();
    }
    /**
     * 取得园级数据
     * @param boolean $return 是否返回数据，如果不是返回，则会直接输出Ajax JSON数据
     * @return void|array
     */
    public function getGradenList() {
        $number=$_GET["number"];
        $year=$_GET["year"];
        $type=$_GET["type"];
        $where=array(
            'type'=>3,
            'fid'=>$this->user["uid"]
        );
        if($number){
           $where['test_type']=$number;
        }
        if($year){


           $where['year']=$year;
        }
        if($type){
            $where['is_report']=$type==1 ? 1 :0;
        }
        $data = D("healthReport")->where($where)->order('year desc')->findPage(20);
        foreach($data['data'] as $key => $value) {
            $reportData=M("report_data")->where("rpt_id=".$value["rpt_id"])->select();
            $data['data'][$key]["sum"]=count($student);
            $data['data'][$key]["number"]=count($reportData);
        }
        if ($data['data']) {
            $this->assign('listData', $data['data']);
            $this->assign('number',$number);
            $this->assign('year',$year);
            $html = $this->fetch('Graden_list');
        } else {
            $html = $this->fetch('Graden_list');
        }
        $data['data'] = $html;
        echo json_encode($data);
        exit();
    }
    /**
     * 取得园级数据
     * @param boolean $return 是否返回数据，如果不是返回，则会直接输出Ajax JSON数据
     * @return void|array
     */
    public function getAreaList() {
        $number=$_GET["number"];
        $year=$_GET["year"];
        $where=array(
            'type'=>array("lt",3),
            'fid'=>$this->user["uid"]
        );
        if($number){
           $where['test_type']=$number;
        }
        if($year){
           $where['year']=$year;
        }
        $data = D("healthReport")->where($where)->order('year desc')->findPage(20);
        foreach($data['data'] as $key => $value) {
            $reportData=M("report_data")->where("rpt_id=".$value["rpt_id"])->select();
            $data['data'][$key]["sum"]=count($student);
            $data['data'][$key]["number"]=count($reportData);
        }
        if ($data['data']) {
            $this->assign('listData', $data['data']);
            $this->assign('number',$number);
            $this->assign('year',$year);
            $html = $this->fetch('Area_list');
        } else {
            $html = $this->fetch('Area_list');
        }
        $data['data'] = $html;
        echo json_encode($data);
        exit();
    }
    /**
     * 取得班级测试/肥胖儿数据
     * @param boolean $return 是否返回数据，如果不是返回，则会直接输出Ajax JSON数据
     * @return void|array
     */
    public function getReportList() {
        $rpt_id=intval($_GET["rpt_id"]);
        $is_obesity=intval($_GET["is_obesity"]);
        $is_unable_ratings=intval($_GET["is_unable_ratings"]);
        $health_report=D("HealthReport")->where('rpt_id='.$rpt_id)->find();
        $data= M("report_data")->where("rpt_id=".$rpt_id)->select();
        foreach($data as $key => $value) {
            $test_data=D("TestData")->where("is_del=0 and test_id=".$value["tid"].' and year='.$health_report['year'])->find();
            $student=D("student")->where("stu_id=".$test_data["stu_id"])->find();
            if(!$student){
                unset($data[$key]);
            }else{
                $data[$key]["name"]=$student["stu_name"];
                $map=array(
                    'fid'=>$student['stu_id'],
                    'year'=>$health_report['year'],
                    'test_type'=>$health_report['test_type'],
                    'is_report'=>$health_report['is_report'],
                );
                $studentReport=D("HealthReport")->where($map)->find();
                $data[$key]["url"]=U('public/Report/Report',array('rpt_id'=>$studentReport['rpt_id']));
                $data[$key]["gender"]=$student["gender"]==1 ? "男" : "女";
                $report_result=model("CalculationMethod")->getDeterminationStandard($test_data,$student["gender"],$student["birthday"]);
                if($report_result){
                    $data[$key]["score"]=$report_result["sum"]."分";
                    if($report_result["result"]==0){
                        $data[$key]["result"]="无法评级";
                    }
                    if($report_result["result"]==4){
                        $data[$key]["result"]="四级（不合格）";
                    }
                    if($report_result["result"]==3){
                        $data[$key]["result"]="三级（合格）";
                    }
                    if($report_result["result"]==2){
                        $data[$key]["result"]="二级（良好）";
                    }
                    if($report_result["result"]==1){
                        $data[$key]["result"]="一级（优秀）";
                    }
                    if($is_obesity){
                        if($report_result["is_obesity"]<4){
                            unset($data[$key]);
                        }
                        if($report_result["is_obesity"]==4){
                            $data[$key]["obesity"]="较胖";
                        }
                        if($report_result["is_obesity"]==5){
                            $data[$key]["obesity"]="很胖";
                        }
                    }
                    if($is_unable_ratings){
                        if($report_result["result"]!=0){
                            unset($data[$key]);
                        }
                        if($report_result["continuousJump"]==0){
                            $data[$key]["rating"].="灵敏协调 ";
                        }
                        if($report_result["balanceBeam"]==0){
                           $data[$key]["rating"].="平衡能力 ";
                        }
                        if($report_result["tennis_throw"]==0){
                            $data[$key]["rating"].="上肢力量 ";
                        }
                        if($report_result["standing_long_jump"]==0){
                            $data[$key]["rating"].="下肢力量 ";
                        }
                        if($report_result["reentry_run"]==0){
                            $data[$key]["rating"].="速度素质 ";
                        }
                        if($report_result["flexion"]==0){
                            $data[$key]["rating"].="柔韧素质 ";
                        }
                    }
                }
            }
        }
        echo json_encode($data);
        exit();
    }
    /**
     * 取得园级无法评级/肥胖儿数据
     * @param boolean $return 是否返回数据，如果不是返回，则会直接输出Ajax JSON数据
     * @return void|array
     */
    public function getGradenStuList() {
        $rpt_id=intval($_GET["rpt_id"]);
        $is_obesity=intval($_GET["is_obesity"]);
        $is_unable_ratings=intval($_GET["is_unable_ratings"]);
        $health_report= D("HealthReport")->where("rpt_id=".$rpt_id)->find();
        $school=M("school")->where("fid=".$health_report['fid'])->find();

        $sql = 'select s.stu_id,s.stu_name,s.gender,s.birthday,t.heart_rate,t.date,t.height,t.weight,t.reentry_run,t.standing_long_jump,t.tennis_throw,t.continuousJump,t.flexion,t.balanceBeam from '.C('DB_PREFIX').'student s join '.C('DB_PREFIX').'test_data t on s.stu_id=t.stu_id where t.is_del=0 and t.year='.$health_report['year'] .' and t.test_number='.$health_report['test_type'].' and t.is_report='.$health_report['is_report'].' and s.school_id =' . $school['id'];
        $data = M()->query($sql);
        foreach ($data as $key => $value) {
            $map=array(
                'fid'=>$value['stu_id'],
                'year'=>$health_report['year'],
                'test_type'=>$health_report['test_type'],
                'is_report'=>$health_report['is_report'],
            );
            $studentReport= M("HealthReport")->where($map)->find();
            $data[$key]["name"]=$value["stu_name"];
            $data[$key]["url"]=U('public/Report/studentReport',array('rpt_id'=>$studentReport['rpt_id']));
            $data[$key]["gender"]=$value["gender"]==1 ? "男" : "女";
            $report_result=model("CalculationMethod")->getDeterminationStandard($value,$value["gender"],$value["birthday"]);
            $data[$key]["score"]=$report_result["sum"]."分";
            if($report_result["result"]==0){
                $data[$key]["result"]="无法评级";
            }
            if($report_result["result"]==4){
                $data[$key]["result"]="四级（不合格）";
            }
            if($report_result["result"]==3){
                $data[$key]["result"]="三级（合格）";
            }
            if($report_result["result"]==2){
                $data[$key]["result"]="二级（良好）";
            }
            if($report_result["result"]==1){
                $data[$key]["result"]="一级（优秀）";
            }
            if($is_obesity>0){
                if($report_result["is_obesity"]<4){
                    unset($data[$key]);
                }
                if($report_result["is_obesity"]==4){
                    $data[$key]["obesity"]="较胖";
                }
                if($report_result["is_obesity"]==5){
                    $data[$key]["obesity"]="很胖";
                }
            }
            if($is_unable_ratings>0){
                if($report_result["result"]!=0){
                    unset($data[$key]);
                }
                if($report_result["continuousJump"]==0){
                    $data[$key]["rating"].="灵敏协调 ";
                }
                if($report_result["balanceBeam"]==0){
                   $data[$key]["rating"].="平衡能力 ";
                }
                if($report_result["tennis_throw"]==0){
                    $data[$key]["rating"].="上肢力量 ";
                }
                if($report_result["standing_long_jump"]==0){
                    $data[$key]["rating"].="下肢力量 ";
                }
                if($report_result["reentry_run"]==0){
                    $data[$key]["rating"].="速度素质 ";
                }
                if($report_result["flexion"]==0){
                    $data[$key]["rating"].="柔韧素质 ";
                }
            }
        }
        echo json_encode($data);
        exit();
    }
    //园级正常报告
    public function Graden(){
        $this->assign("year",$this->year);
        $this->display();
    }
    //市区级报告
    public function Area(){
        //此功能用于生成市区级报告
        // $user=model("User")->where("uid=".$this->user['uid'])->field("city,area,fid,uid")->find();
        // if($this->userGroup==3){
        //     $where["area"]=$user["area"];
        // }else{
        //     $where["city"]=$user["city"];
        // }
        // $school=M("school")->where($where)->field("id")->select();
        // if($school){
        //     $school=getSubByKey($school,"id");
        //     $student=M("student")->where(array("school_id"=>array("in",$school)))->field("stu_id")->select();
        //     $student=getSubByKey($student,"stu_id");
        // }  
        // foreach ($this->year as $key => $value) {
        //    for ($i=2; $i>0; $i--) { 
               //  $map=array(
               //      'year'=>"2014",
               //      'test_number'=>$i,
               //      'type'=>array("lt",3),
               //      'fid'=>$this->user["uid"]
               //  );
               //  $result=D("HealthReport")->where($map)->find();
               // if(!$result && $student){
               //      // 当数据量过大时需手动修改年份和test_number值，避免报502错误
               //          $sql = 'select s.stu_id,t.test_id,t.test_number,t.is_report,t.year from '.C('DB_PREFIX').'student s join '.C('DB_PREFIX').'test_data t on s.stu_id=t.stu_id where t.is_report=1 and  t.year='.$value.' and t.test_number='.$i.' and t.is_report=1 and s.stu_id in(' . trim(implode(',' , $student),',') . ') ';
               //          $test_data = M()->query($sql);
               //          if($test_data){
               //              $area_type=$this->userGroup == 3 ? 2 : 1;
               //              foreach ($test_data as $k => $v) {
               //                 D("HealthReport")->addOtherReport($v,$v["test_id"],$this->user['uid'],$area_type);
               //              }
               //          }
               // }
        //   }
        // }
        $this->assign("year",$this->year);
        $this->display();
    }
    public function Graden_View(){

	$rpt_id = $_GET['rpt_id'];
/*
	$rpt_id_map = array(
		213781 => 196945,
		213780 => 176271,
		213778 => 126930,
		213779 => 126933,
		213777 => 70669,
		//
		213787 => 196945,
		213786 => 176271,
		213784 => 126930,
		213785 => 126933,
		213783 => 70669,
	);
*/
        $health_report=D("HealthReport")->where("rpt_id=".$rpt_id)->find();
        $yeararr=array(
            '0'=>$health_report['year']-2,
            '1'=>$health_report['year']-1,
            '2'=>intval($health_report['year']),
        );
        for ($i=0; $i <count($yeararr); $i++) { 
            $test_data=D("HealthReport")->getReportData($yeararr[$i],$health_report,3,1);
            $test_data['year']=$yeararr[$i];
            $data[]=$test_data;
        }

        $school=M("school")->where("city=".$this->user["city"])->select();
        $school["sum"]=count($school);
        $this->assign('health_report',$health_report);
        $this->assign('data',$data);
        $this->assign('rpt_id',intval($_GET["rpt_id"]));
        $this->assign('type',$health_report["type"]);
        $this->assign('school',$school);
        $this->assign('yeararr' ,$yeararr );
        $this->display();
    }
    //园级肥胖儿报告
    public function GradenObesity(){
        $rpt_id=intval($_GET["rpt_id"]);
        $HealthReport=D("HealthReport")->where("rpt_id=".$rpt_id)->field("rpt_name,type")->find();
        $this->assign('rpt_name',$HealthReport["rpt_name"]);
        $this->assign('rpt_id',$rpt_id);
        $this->assign('type',$HealthReport["type"]);
        $this->display();
    }
    //园级无法评级报告
    public function GradenNORatings(){
        $rpt_id=intval($_GET["rpt_id"]);
        $HealthReport=D("HealthReport")->where("rpt_id=".$rpt_id)->field("rpt_name,type")->find();
        $this->assign('rpt_name',$HealthReport["rpt_name"]);
        $this->assign('rpt_id',$rpt_id);
        $this->assign('type',$HealthReport["type"]);
        $this->display();
    }
    //正常报告
    public function NormalReport(){
        if($_GET["rpt_id"]){
            $where["rpt_id"]=intval($_GET["rpt_id"]);
        }else{
            if($this->userGroup==2){
                $type=1;
            }else{
                $type=2;
            }
            $where=array(
                'type'=>$type,
                'fid'=>$this->user["uid"],
                'year'=>date("Y",time() ),
            );  
        }
        $health_report=D("HealthReport")->where($where)->order("rpt_id desc")->find();
        if(!$health_report){
            $this->assign('jumpUrl', U('public/Passport/logout'));
            $this->success("<div style='height:150px;text-align:center;line-height:150px;font-size:24px;color:#32adf9;'>没有获取到当前用户的相关报告，请重新登录！</div>");
            exit();
        }
        $yeararr=array(
            '0'=>$health_report['year']-2,
            '1'=>$health_report['year']-1,
            '2'=>intval($health_report['year']),
        );
        for ($i=0; $i <count($yeararr); $i++) { 
            $test_data=D("HealthReport")->getReportData($yeararr[$i],$health_report,$health_report["type"],1);
            $test_data['year']=$yeararr[$i];
            $data[]=$test_data;
        }
        //查询当前地区所有幼儿园总数
        $school=D("HealthReport")->getSchoolSum($health_report["fid"]);
        $this->assign('sum',$school["sum"]);
        $this->assign("rpt_name",$school["uname"]);
        $this->assign("rpt_id",$health_report["rpt_id"]);
        $this->assign("type",$health_report["type"]);
        $this->assign('health_report',$health_report);
        $this->assign('data',$data);
        $this->assign('yeararr' ,$yeararr );
        $this->display();
    }
    //无法统计报告
    public function NoRatingReport(){
        $where["rpt_id"]=intval($_GET["rpt_id"]);
        $health_report=D("HealthReport")->where($where)->find();
        $yeararr=array(
            '0'=>$health_report['year']-2,
            '1'=>$health_report['year']-1,
            '2'=>intval($health_report['year']),
        );
        for ($i=0; $i <count($yeararr); $i++) { 
            $test_data=D("HealthReport")->getReportData($yeararr[$i],$health_report,$health_report["type"],2);
            $test_data['year']=$yeararr[$i];
            $data[]=$test_data;
        }
        //查询当前地区所有幼儿园总数
        $school=D("HealthReport")->getSchoolSum($health_report["fid"]);
        $this->assign('sum',$school["sum"]);
        $this->assign("rpt_name",$school["uname"]);
        $this->assign('health_report',$health_report);
        $this->assign('data',$data);
        $this->assign('rpt_id',intval($_GET["rpt_id"]));
        $this->assign('type',$health_report["type"]);
        $this->assign('yeararr' ,$yeararr );
        $this->display();
    }
    //肥胖儿报告
    public function ObesityReport(){
        $where["rpt_id"]=intval($_GET["rpt_id"]);
        $health_report=D("HealthReport")->where($where)->find();
        $yeararr=array(
            '0'=>$health_report['year']-2,
            '1'=>$health_report['year']-1,
            '2'=>intval($health_report['year']),
        );
        for ($i=0; $i <count($yeararr); $i++) { 
            $test_data=D("HealthReport")->getReportData($yeararr[$i],$health_report,$health_report["type"],3);
            $test_data['year']=$yeararr[$i];
            $data[]=$test_data;
        }
        //查询当前地区所有幼儿园总数
        $school=D("HealthReport")->getSchoolSum($health_report["fid"]);
        $this->assign('sum',$school["sum"]);
        $this->assign("rpt_name",$school["uname"]);
        $this->assign('health_report',$health_report);
        $this->assign('data',$data);
        $this->assign('rpt_id',intval($_GET["rpt_id"]));
        $this->assign('type',$health_report["type"]);
        $this->assign('yeararr' ,$yeararr );
        $this->display();
    }
    public function deleteReport(){
        $rpt_id=intval($_GET["rpt_id"]);
        $res=M("health_report")->where("rpt_id=".$rpt_id)->delete();
        if($res){
            M("report_data")->where("rpt_id=".$rpt_id)->delete();
            echo $res;exit();
        }else{
            echo "操作失败，请重试";exit();
        }
    }
    //院所列表
    public function GradenList(){
        $rpt_id=intval($_GET["rpt_id"]);
        $health_report=D("HealthReport")->where("rpt_id=".$rpt_id)->find();
        $map=array(
            'year'=>$health_report['year'],
            'is_report'=>$health_report['is_report'],
            'test_type'=>$health_report['test_type'],
        );
        $user=model("User")->where("uid=".$health_report["fid"])->field("uid,area")->find();
        if($health_report["type"]==1){
            $map["type"]=2;
            $user_list=model("User")->where("fid=".$user["uid"])->select();
            $uids=getSubByKey($user_list,"uid");
            $map["fid"]=array('in',$uids);
        }else{
            $where["area"]=$user["area"];
            $map["type"]=3;
            $school=M("school")->where($where)->field("fid")->select();
            $school_ids=getSubByKey($school,"fid");
            $map["fid"]=array('in',$school_ids);
        }
        $data=D("health_report")->where($map)->select();
        foreach ($data as $key => $value) {
            if($value['type']==2){
                $data[$key]["url"]=U('public/Report/NormalReport',array('rpt_id'=>$value['rpt_id']));
            }else{
               $data[$key]["url"]=U('public/Report/Graden_View',array('rpt_id'=>$value['rpt_id'])); 
            }
        }
        $this->assign("data",$data);
        $this->assign("rpt_id",$rpt_id);
        $this->assign("type",$health_report["type"]);
        $this->assign("rpt_name",$health_report["rpt_name"]);
        $this->display();
    }
}              
